using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Cube : MonoBehaviour 
{
	public bool m_Link;
    public bool noGravity;

	
	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
    void FixedUpdate()
    {
        this.rigidbody.WakeUp();

        if (m_Link)
        {
            for (int i = 0; i < Character.Instance.m_linked.Count; i++)
            {
                float length;
                Vector3 forceDirection = Character.Instance.m_linked[i].transform.position - transform.position;
                length = forceDirection.magnitude;
                forceDirection.Normalize();

                if (length != 0)
                {
                    this.gameObject.rigidbody.AddForce(forceDirection * (1.0f / length) * Character.Instance.m_ValGravity);
                }
            }
        }
    }
	
	void Update ()
	{
       
	}

    void OnTriggerEnter(Collider element)
    {
        if (element.tag == "Projectile")
        {
            switch (Character.Instance.m_ModeProjectile)
            {
                case ProjectileBehavior.Nullify : 

                    if (noGravity == true)
                    {
                        rigidbody.useGravity = true;
                        noGravity = false;
                        renderer.material = (Material)Resources.Load("Material/m_Cube");
                        
                    }
                    else
                    {
                        rigidbody.useGravity = false;
                        renderer.material = (Material)Resources.Load("Material/m_NoGravity");
                        noGravity = true;
                    }
                    Destroy(element.gameObject);

                break;

                case ProjectileBehavior.Link :

                    if (m_Link == false)
                    {
                        Character.Instance.m_linked.Add(gameObject);
                        renderer.material = (Material)Resources.Load("Material/m_LinkElement");
                        m_Link = true;
                        Destroy(element.gameObject);
                    }
                    else
                    {
                        m_Link = false; ;
                        Character.Instance.m_linked.Remove(gameObject);
                        renderer.material = (Material)Resources.Load("Material/m_Cube");
                    }

                break;

                case ProjectileBehavior.Static :

                if (transform.rigidbody.isKinematic)
                {
                    transform.rigidbody.isKinematic = false;
                }
                else 
                {
                    transform.rigidbody.isKinematic = true;
                }
                break;

            }
           
                
            }
        }
    }
	
			


